package com.kc.business.service.impl;

import com.kc.business.domain.BizScenicBusiness;
import com.kc.business.domain.dto.BizBusinessDTO;
import com.kc.business.enums.BusinessTypeEnum;
import com.kc.business.mapper.BizScenicBusinessMapper;
import com.kc.business.service.IBizBusinessGoodsService;
import com.kc.business.service.IBizGoodsService;
import com.kc.business.service.IBizScenicBusinessService;
import com.kc.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/9
 */
@Service
@Slf4j
public class BizScenicBusinessServiceImpl implements IBizScenicBusinessService {

    @Autowired
    BizScenicBusinessMapper bizScenicBusinessMapper;
    @Resource
    private IBizGoodsService bizGoodsService;
    @Resource
    private IBizBusinessGoodsService businessGoodsService;


    @Override
    public List<BizScenicBusiness> getList(BizScenicBusiness bizScenicBusiness) {
        List<BizScenicBusiness> scenicBusinesses = bizScenicBusinessMapper.select(bizScenicBusiness);
        for(BizScenicBusiness business : scenicBusinesses){
            if(business.getMainTypeId().equals(3)){
                business.setGoodsNum(bizGoodsService.getPlatformGoodsNum(business.getId(), BusinessTypeEnum.BUSINESS.getValue()));
            }else{
                business.setGoodsNum(businessGoodsService.getGoodsNum(business.getId()));
            }
        }
        return scenicBusinesses;
    }

    /**
     * 修改商家
     *
     * @param bizScenicBusiness
     * @return
     */
    @Override
    public int edit(BizScenicBusiness bizScenicBusiness) {
        bizScenicBusiness.setIsPersonalInfoPerfected(1);
        return bizScenicBusinessMapper.updateByPrimaryKeySelective(bizScenicBusiness);
    }

    @Override
    public List<BizBusinessDTO> getRuleBusinessListByUserId(Integer userId) {
        BizScenicBusiness bizScenicBusiness = new BizScenicBusiness();
        bizScenicBusiness.setUserId(userId);
        //bizScenicBusiness.setMainTypeId(3);
        List<BizScenicBusiness> businessList = getList(bizScenicBusiness);
        List<BizBusinessDTO> businessDTOList = new ArrayList<>();
        for(BizScenicBusiness scenicBusiness : businessList){
            BizBusinessDTO bizBusinessDTO = new BizBusinessDTO();
            bizBusinessDTO.setBusinessId(scenicBusiness.getId());
            bizBusinessDTO.setBusinessName(scenicBusiness.getName());
            bizBusinessDTO.setIsBusiness(scenicBusiness.getIsBusiness());
            bizBusinessDTO.setBusinessStatus(scenicBusiness.getIsBusiness().equals(1)?"营业中":"未营业");
            bizBusinessDTO.setMainTypeId(scenicBusiness.getMainTypeId());
            businessDTOList.add(bizBusinessDTO);
        }
        return businessDTOList;
    }

    @Override
    public BizScenicBusiness getBusinessInfoById(Integer id) {
        BizScenicBusiness bizScenicBusiness = bizScenicBusinessMapper.selectByPrimaryKey(id);
        bizScenicBusiness.setImages(StringUtils.split(bizScenicBusiness.getImage(),","));
        if(bizScenicBusiness.getImages() == null){
            String[] arr = new String[0];
            bizScenicBusiness.setImages(arr);
        }
        return bizScenicBusiness;
    }
}
